package com.huanying.goodjob.paymentactivity;

import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.payment.PaymentService;
import com.huanying.framework.payment.Trade_Money;
import com.huanying.framework.user.User;
import com.huanying.framework.utils.Date;

@Service
public class PaymentactivityServiceImpl implements PaymentactivityService {
	@Autowired
	private BaseDao dao;
	//记录转账交易
	@Override
	public void moneyRecord(int user_id,int userobject_id,double amount,int direction,int bankType) throws Exception{
		//主动方用户ID，被动方用户ID，交易金额，交易方向（1支出 2收入），交易类型（1 平台内交易 ，转账2平台外交易，提现，充值），交易工具（1支付宝）
		//String hql = new String("from User u where u.id=?");
		if(direction==1) //支出
			{
			Trade_Money trade_money1 = new Trade_Money();
			User user=(User) dao.load(User.class, user_id);
			User userobject=(User) dao.load(User.class, userobject_id);
		
			trade_money1.setUser(user);
			trade_money1.setUserobject(userobject);
			trade_money1.setAmount(amount);
			//trade_money1.setTransactiontime(DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s')); new Timestamp(date.getTime());
			Date date = new Date();
			trade_money1.setTransactiontime(new Timestamp(date.getTime()));
			trade_money1.setDirection(1);
			trade_money1.setType(1);
			trade_money1.setCreate_time(new Timestamp(date.getTime()));
			trade_money1.setBankType(bankType);
			dao.save(trade_money1);//保存支出方的交易记录
			
			user.setMoney(user.getMoney()-amount);
			dao.update(user);//更新支出方用户的现有金额
			userobject.setMoney(userobject.getMoney()+amount);
			dao.update(userobject);//更新收入方用户的现有金额
		}
		else//收入
		{
			Trade_Money trade_money2 = new Trade_Money();
			User user=(User) dao.load(User.class, user_id);
			User userobject=(User) dao.load(User.class, userobject_id);
		
			trade_money2.setUser(user);
			trade_money2.setUserobject(userobject);
			trade_money2.setAmount(amount);
			Date date = new Date();
			trade_money2.setTransactiontime(new Timestamp(date.getTime()));
			trade_money2.setDirection(2);
			trade_money2.setType(1);
			trade_money2.setCreate_time(new Timestamp(date.getTime()));
			trade_money2.setBankType(bankType);
			dao.save(trade_money2);//保存收入方的交易记录
			
			user.setMoney(user.getMoney()+amount);
			dao.update(user);//更新支出方用户的现有金额
			userobject.setMoney(userobject.getMoney()-amount);
			dao.update(userobject);//更新收入方用户的现有金额
		}
	}

	
    //记录充值交易
	@Override
	public void rechargeRecord(int user_id,double money,int banktype) throws Exception{
		//更新trade_money表
		//Trade_Money trade_money=(Trade_Money) dao.load(User.class, user_id);
		User user=(User) dao.load(User.class, user_id);
		Trade_Money trade_money=new Trade_Money();
		trade_money.setUser(user);
		trade_money.setDirection(1);
		trade_money.setType(2);
		trade_money.setAmount(money);
		trade_money.setBankType(banktype);
		Date date = new Date();
		trade_money.setTransactiontime(new Timestamp(date.getTime()));
		trade_money.setCreate_time(new Timestamp(date.getTime()));
		dao.save(trade_money);
		//更新user表
		double money_user=user.getMoney();
		double money_user_user_new=money_user-money;
		user.setMoney(money_user_user_new);
		dao.update(user);
	}
	//记录提现交易
	@Override
	public void withdraw_cashRecord(int user_id,double money,int banktype)throws Exception{
		//更新trade_money表
		User user=(User) dao.load(User.class, user_id);
		Trade_Money trade_money=new Trade_Money();
		trade_money.setUser(user);		
		trade_money.setDirection(2);
		trade_money.setType(2);
		trade_money.setAmount(money);
		trade_money.setBankType(banktype);
		Date date = new Date();
		trade_money.setTransactiontime(new Timestamp(date.getTime()));
		trade_money.setCreate_time(new Timestamp(date.getTime()));
		dao.save(trade_money);
		//更新user表
		double money_user=user.getMoney();
		double money_user_user_new=money_user+money;
	    user.setMoney(money_user_user_new);
		dao.update(user);
	}
}
